<?php
// +----------------------------------------------------------------------
// |     【PurchaseInfo】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class PurchaseInfo extends Model{
    protected static $prefix;
     
    public function __construct(array $data = []){
        parent::__construct($data);
        self::$prefix = env('database.prefix'); // 获取表名前缀
    }
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');
        
        $list = self::alias('info')
         // 关联采购表
         ->leftjoin(self::$prefix . 'purchase_main main', 'info.pur_id = main.id')
         // 关联员工表
         ->leftJoin(self::$prefix . 'staff_main staff', 'main.staff_id = staff.id')
         // 关联面料表
         ->leftJoin(self::$prefix . 'fabric_name fabric', 'main.fabric_id = fabric.id')
         // 按条件查询
         ->where($where)
         // 指定字段
         ->field("info.*,main.pur_sn, FROM_UNIXTIME(main.create_time, '%Y-%m-%d %H:%i') as create_time2,main.create_time, staff.name as staff_name,staff.short_name,staff.store_name,fabric.sup_empty")
         // 排序
         ->order([
             'main.create_time' => 'desc',
         ])
         // 分页
         ->paginate($limit);
         
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($id,$where=[],$order="",$more=true,$n_page=0,$limit=50){
		if(is_array($id)){
			$where[]=['pur_id','in',$id];
		}else{
			$where[]=['pur_id','=',$id];
		}
		if($more){			//最佳供货商信息
			$data=self::alias('info')
			->leftJoin('purchase_main main','info.pur_id=main.id')
            ->leftJoin('fabric_main fabric','info.fabric_id=fabric.id')
			->field('info.*,fabric.sup_empty')
			->order($order)
			->where($where);
		}else{
			$data=self::order($order)->where($where);
		}
		
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();

		return $data;
	}
    
    // 主表和子表合并扩展
    public static function listMerge($id,$where=[],$order="",$more=true,$n_page=0,$limit=50){
    	if(is_array($id)){
    		$where[]=['pur_id','in',$id];
    	}else if(is_int($id) && $id>0){
    		$where[]=['pur_id','=',$id];
    	}
    	if($more){			//最佳供货商信息
    		$data=self::alias('info')
    		->leftJoin('purchase_main main','info.pur_id=main.id')
            ->leftJoin('fabric_main fabric','info.fabric_id=fabric.id')
            ->leftJoin('staff_main staff','main.staff_id=staff.id')
    		->field('info.*,fabric.sup_empty,staff.name as staff_name')
    		->order($order)
    		->where($where);
    	}else{
    		$data=self::order($order)->where($where);
    	}
    	
    	if($n_page>0){
    		$data=$data->page($n_page,$limit);
    	}
    	$data=$data->select()->toArray();
    
    	return $data;
    }
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
	
	/** 获取器 **/
	
}
